package com.wohuijituan.whcsmerchant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.adapter.NoticeRecordAdapter;
import com.wohuijituan.whcsmerchant.bean.NoticeListBean;
import com.wohuijituan.whcsmerchant.callback.NoticeListCallBack;
import com.wohuijituan.whcsmerchant.url.ParamsKey;
import com.wohuijituan.whcsmerchant.url.Urls;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by zsy_18 on 2016/12/27.
 */

public class NoticeRecordActivity extends MyBaseActivity implements View.OnClickListener{
    private final String TAG = "NoticeRecordActivity";
    private PullToRefreshListView pullNoticeRecord;
    private ImageButton titleGoBack;
    private TextView titleName;
    private TextView titleTvAction;
    private ImageButton titleIbAction;

    private void assignViews() {
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleName = (TextView) findViewById(R.id.title_name);
        titleTvAction = (TextView) findViewById(R.id.title_tv_action);
        titleIbAction = (ImageButton) findViewById(R.id.title_ib_action);
        pullNoticeRecord = (PullToRefreshListView) findViewById(R.id.pull_notice_record);
    }
    private NoticeRecordAdapter recordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_record);
        assignViews();
        initData();
        initView();
        initListener();
        noticeListRequest();
    }

    private void initData() {
        List<NoticeListBean.DataBean> list = new ArrayList<>();
        recordAdapter = new NoticeRecordAdapter(ctx,list);
        pullNoticeRecord.setAdapter(recordAdapter);
    }

    private void initView() {
        pullNoticeRecord.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        titleGoBack.setVisibility(View.VISIBLE);
        titleName.setText("公告");
        titleTvAction.setVisibility(View.VISIBLE);
        titleTvAction.setText("发布");
    }

    private void initListener() {
        titleGoBack.setOnClickListener(this);
        titleTvAction.setOnClickListener(this);
        pullNoticeRecord.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                noticeListRequest();
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_go_back:
                onBackPressed();
                break;
            case R.id.title_tv_action:
                //  2016/12/27 跳转到发布公告界面
                Intent intent = new Intent(this,ReleaseNoticeActivity.class);
                startActivityForResult(intent,1);
//                startActivity(intent);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==RESULT_OK){
            //当发布公告成功的时候进行刷新公告
            noticeListRequest();
        }
    }
    private void noticeListRequest(){
        String strUrl = Urls.NoticeList;
        params.clear();
        params.addStringRequest(ParamsKey.UserKey,myApplication.getUserKey());
        myOkHttpUtils.postRequest(strUrl, params, new NoticeListCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(ctx,"网络错误!");
                pullNoticeRecord.onRefreshComplete();
            }

            @Override
            public void onResponse(NoticeListBean response, int id) {
                Message msg = new Message();
                msg.obj = response;
                msg.what = NOTICE_LIST_REQUEST_OK;
                handler.sendMessage(msg);
            }
        });
    }
    private static final int NOTICE_LIST_REQUEST_OK = 8523;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case NOTICE_LIST_REQUEST_OK:
                    pullNoticeRecord.onRefreshComplete();
                    // 2017/16  通知列表请求成功
                    NoticeListBean data = (NoticeListBean) msg.obj;
                    if (data.getState()==1){
                        if (data.getData().size()!=0){
                            recordAdapter.replace(data.getData());
//                            showToast(ctx,"chenggong");
                        }else {
                            showToast(ctx,"暂无公告!");
                        }
                    }else {
                        showToast(ctx,data.getMsg()+"");
                    }
                    break;
            }
        }
    };
}
